package com.itjiange.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 接受验证码邮件
 *
 * @author ItJiange
 * @date 2023/10/19
 */
@WebServlet("/receiveEmail")
public class ReceiveEmailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String reqRandomCode = req.getParameter("randomCode");
        // 验证码
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            String value = cookie.getValue();
            if ("RANDOM_CODE".equals(name)) {
                if (!reqRandomCode.equals(value)) {
                    System.out.println("验证码输入错误: " + value);
                    resp.getWriter().write("<h1>失败<h1>");
                    return;
                }
                System.out.println("验证码输入正确: " + value);
                resp.getWriter().write("<h1>成功<h1>");
                return;
            }
        }
    }
}
